home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
367_01
/
futi14as.zoo
/
dd.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-22
|
28KB
|
1,036 lines
/* dd -- convert a file while copying it.
Copyright (C) 1985, 1989, 1990 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Written by Paul Rubin and David MacKenzie. */
/* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
This port is also distributed under the terms of the
GNU General Public License as published by the
Free Software Foundation.
Please note that this file is not identical to the
original GNU release, you should have received this
code as patch to the official release. */
#ifdef MSDOS
static char RCS_Id[] =
"$Header: e:/gnu/fileutil/RCS/dd.c 1.4.0.2 90/09/19 11:17:53 tho Exp $";
static char Program_Id[] = "dd";
static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
#define VERSION \
"GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
(sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
#define COPYING \
"This is free software, distributed under the terms of the\n" \
"GNU General Public License. For details, see the file COPYING.\n"
#endif /* MSDOS */
/* Options:
Numbers can be followed by a multiplier:
b=512, k=1024, w=2, xm=number m
if=FILE Read from FILE instead of stdin.
of=FILE Write to FILE instead of stdout; don't
truncate FILE.
ibs=BYTES Read BYTES bytes at a time.
obs=BYTES Write BYTES bytes at a time.
bs=BYTES Override ibs and obs.
cbs=BYTES Convert BYTES bytes at a time.
skip=BLOCKS Skip BLOCKS ibs-sized blocks at
start of input.
seek=BLOCKS Skip BLOCKS obs-sized blocks at
start of output.
count=BLOCKS Copy only BLOCKS input blocks.
conv=CONVERSION[,CONVERSION...]
Conversions:
ascii Convert EBCDIC to ASCII.
ebcdic Convert ASCII to EBCDIC.
ibm Convert ASCII to alternate EBCDIC.
block Pad newline-terminated records to size of
cbs, replacing newline with trailing spaces.
unblock Replace trailing spaces in cbs-sized block
with newline.
lcase Change uppercase characters to lowercase.
ucase Change lowercase characters to uppercase.
swab Swap every pair of input bytes.
Unlike the Unix dd, this works when an odd
number of bytes are read.
noerror Continue after read errors.
sync Pad every input block to size of ibs with
trailing NULs. */
#ifdef MSDOS
/*
im={text,binary} Input file translation mode (default: text)
om={text,binary} Output file translation mode (default: text)
*/
#endif
#include <stdio.h>
#include <ctype.h>
#ifdef STDC_HEADERS
#define ISLOWER islower
#define ISUPPER isupper
#else
#define ISLOWER(c) (isascii ((c)) && islower ((c)))
#define ISUPPER(c) (isascii ((c)) && isupper ((c)))
#endif
#include <sys/types.h>
#include <signal.h>
#include "system.h"
#ifdef STDC_HEADERS
#include <errno.h>
#include <stdlib.h>
#else
char *malloc ();
extern int errno;
#endif
#ifndef _POSIX_SOURCE
long lseek ();
#endif
#define equal(p, q) (strcmp ((p),(q)) == 0)
#ifndef MSDOS
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
/* Default input and output blocksize. */
#define DEFAULT_BLOCKSIZE 512
/* Conversions bit masks. */
#define C_ASCII 01
#define C_EBCDIC 02
#define C_IBM 04
#define C_BLOCK 010
#define C_UNBLOCK 020
#define C_LCASE 040
#define C_UCASE 0100
#define C_SWAB 0200
#define C_NOERROR 0400
#define C_SYNC 01000
/* Use separate input and output buffers, and combine partial input blocks. */
#define C_TWOBUFS 04000
#ifdef MSDOS
#include <io.h>
extern void error (int status, int errnum, char *message, ...);
extern void main (int argc, char **argv);
static void copy (void);
static void scanargs (int argc, char **argv);
static int parse_integer (char *str);
static void parse_conversion (char *str);
static void apply_translations (void);
static void translate_charset (unsigned char *new_trans);
static int bit_count (unsigned int i);
static void print_stats (void);
static void quit (int code);
static SIGTYPE interrupt_handler (void);
static char *xmalloc (unsigned short n);
static void usage (char *string, char *arg0, char *arg1);
#else /* not MSDOS */
char *xmalloc ();
SIGTYPE interrupt_handler ();
int bit_count ();
int parse_integer ();
void apply_translations ();
void copy ();
void error ();
void parse_conversion ();
void print_stats ();
void translate_charset ();
void quit ();
void scanargs ();
void usage ();
#endif /* not MSDOS */
/* The name this program was run with. */
char *program_name;
/* The name of the input file, or NULL for the standard input. */
char *input_file = NULL;
/* The input file descriptor. */
int input_fd = 0;
/* The name of the output file, or NULL for the standard output. */
char *output_file = NULL;
/* The output file descriptor. */
int output_fd = 1;
/* The number of bytes in which atomic reads are done. */
#ifdef MSDOS
int input_blocksize = -1;
#else
long input_blocksize = -1;
#endif
/* The number of bytes in which atomic writes are done. */
#ifdef MSDOS
int output_blocksize = -1;
#else
long output_blocksize = -1;
#endif
/* Conversion buffer size, in bytes. 0 prevents conversions. */
#ifdef MSDOS
int conversion_blocksize = 0;
#else
long conversion_blocksize = 0;
#endif
/* Skip this many records of `input_blocksize' bytes before input. */
int skip_records = 0;
/* Skip this many records of `output_blocksize' bytes before output. */
#ifdef MSDOS
int seek_record = 0;
#else
long seek_record = 0;
#endif
/* Copy only this many records. <0 means no limit. */
int max_record = -1;
/* Bit vector of conversions to apply. */
int conversions_mask = 0;
/* Number of partial blocks written. */
unsigned w_partial = 0;
/* Number of full blocks written. */
unsigned w_full = 0;
/* Number of partial blocks read. */
unsigned r_partial = 0;
/* Number of full blocks read. */
unsigned r_full = 0;
/* Records truncated by conv=block. */
unsigned r_truncate = 0;
#ifdef MSDOS
/* Translation modes */
int input_file_mode = O_TEXT;
int output_file_mode = O_TEXT;
#endif
/* Output representation of newline and space characters. */
unsigned char newline_character = '\n';
unsigned char space_character = ' ';
struct conversion
{
char *convname;
int conversion;
};
struct conversion conversions[] =
{
"ascii", C_ASCII | C_TWOBUFS, /* EBCDIC to ASCII. */
"ebcdic", C_EBCDIC | C_TWOBUFS, /* ASCII to EBCDIC. */
"ibm", C_IBM | C_TWOBUFS, /* Slightly different ASCII to EBCDIC. */
"block", C_BLOCK | C_TWOBUFS, /* Variable to fixed length records. */
"unblock", C_UNBLOCK | C_TWOBUFS, /* Fixed to variable length records. */
"lcase", C_LCASE | C_TWOBUFS, /* Translate upper to lower case. */
"ucase", C_UCASE | C_TWOBUFS, /* Translate lower to upper case. */
"swab", C_SWAB | C_TWOBUFS, /* Swap bytes of input. */
"noerror", C_NOERROR, /* Ignore i/o errors. */
"sync", C_SYNC, /* Pad input records to ibs with NULs. */
NULL, 0
};
/* Translation table formed by applying successive transformations. */
unsigned char trans_table[256];
unsigned char ascii_to_ebcdic[] =
{
0, 01, 02, 03, 067, 055, 056, 057,
026, 05, 045, 013, 014, 015, 016, 017,
020, 021, 022, 023, 074, 075, 062, 046,
030, 031, 077, 047, 034, 035, 036, 037,
0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,